package com.example.hasee.zhcd.Activities.ShenHe;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.example.hasee.zhcd.Adapters.ShipForecastAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static com.example.hasee.zhcd.R.id.archives_list;
import static com.example.hasee.zhcd.R.id.fshipname;

public class ShipForecastActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private SwipeMenuListView forecast_list;
    private String strFID;
    private String strAccount;
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private ShipForecastAdapter adapter;

    @Override
    public int contentViewId() {
        return R.layout.activity_ship_forecast;
    }

    @Override
    public void set() {
        title.setText("船舶预报审核");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
    }

    @Override
    public void init() {
        strAccount = loginmap.get("account");
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        forecast_list = (SwipeMenuListView) findViewById(R.id.forecast_list);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHSH_CBYBList":
//                Log.e("ZHSH_CBYBList", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                //ZHSH_CBYBList: {"data":[{"fid":"445", "fshipname":"安诚山", "fhc":"12345",
                // "fyddate":"06-06 14:05", "flgmd":"卸货", "fhl":"油类", "fwtf":"中远海运散货运输有限公司",
                // "flx":"乳山"}],"rspcode": 0,"rspmessage":""}
                //E/ZHSH_CBYBList: {"data":"","rspcode":1,"rspmessage":"无数据"}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        forecast_list.setVisibility(View.VISIBLE);
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fid", data.getJSONObject(i).getString("fid"));
                            map.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                            map.put("fhc", data.getJSONObject(i).getString("fhc"));
                            map.put("fyddate", data.getJSONObject(i).getString("fyddate"));
                            map.put("flgmd", data.getJSONObject(i).getString("flgmd"));
                            map.put("fhl", data.getJSONObject(i).getString("fhl"));
                            map.put("fwtf", data.getJSONObject(i).getString("fwtf"));
                            map.put("flx", data.getJSONObject(i).getString("flx"));
                            list.add(map);
                        }
                        adapter = new ShipForecastAdapter(ShipForecastActivity.this, list);
                        forecast_list.setAdapter(adapter);
                        SwipeMenuCreator creater = new SwipeMenuCreator() {
                            @Override
                            public void create(SwipeMenu menu) {
                                SwipeMenuItem deleteItem = new SwipeMenuItem(ShipForecastActivity.this);
                                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                                deleteItem.setWidth(dp2px(90));
                                deleteItem.setTitle("审核通过");
                                deleteItem.setTitleColor(Color.WHITE);
                                deleteItem.setTitleSize(18);
                                menu.addMenuItem(deleteItem);
                            }
                        };

                        forecast_list.setMenuCreator(creater);
                        forecast_list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                                switch (index) {
                                    case 0:
                                        strFID = list.get(position).get("fid");
                                        passForecast();
                                        break;
                                }
                                return false;
                            }
                        });


                    } else {
                        forecast_list.setVisibility(View.GONE);
                        Toast.makeText(ShipForecastActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHSH_CBYBSH":
//                Log.e("ZHSH_CBYBSH", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                //ZHSH_CBYBSH: {"data":"","rspcode": 1,"rspmessage":"true"}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("true".equals(rspmessage)) {
                        Toast.makeText(ShipForecastActivity.this, "审核通过", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(ShipForecastActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getForecastList();
                break;
        }
    }

    private void getForecastList() {
        ProgressDialogUtils.createProgressDialog(ShipForecastActivity.this, "正在获取列表...");
        HashMap<String, String> map = new HashMap<>();
        httpUtils.get("ZHSH_CBYBList", map, "ZHSH_CBYBList");
    }

    private void passForecast() {
        ProgressDialogUtils.createProgressDialog(ShipForecastActivity.this, "正在审核...");
        HashMap<String, String> map = new HashMap<>();
        map.put("strFID", strFID);
        map.put("strAccount", strAccount);
        httpUtils.get("ZHSH_CBYBSH", map, "ZHSH_CBYBSH");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getForecastList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}
